﻿/// <reference path="../jquery-1.5.1-vsdoc.js" />
/// <reference path="../1stLBB/hu8.box.js" />
/// <reference path="../hu8.soso5.admin.js" />
/// <reference path="../3rdParty/Wijmo/Complete/jquery.wijmo-complete.all.2.1.0.min.js" />
/// <reference path="../3rdParty/Wijmo/Open/jquery.wijmo-open.all.2.1.0.min.js" />
/// <reference path="../3rdParty/Wijmo/Complete/js/jquery.wijmo.wijrating.js" />


/*  评分插件UI脚本
*   Author  Chiron
------------------------------------------------*/
(function ($) {
    //初始化评分控件
    var initRatings = function (sel) {
        $(sel || ".uRating-rating").each(function () {
            var $this = $(this).wijrating();
            var o = $.extend(true, {}, $this.data("wijrating").options, {
                rated: function (e, args) {
                    if ($this.attr("rating-value-box")) {
                        $($this.attr("rating-value-box")).html(args.value)
                    }
                }
            });
            if ($this.attr("rating-min")) o.min = parseFloat($this.attr("rating-min"));
            if ($this.attr("rating-max")) o.max = parseFloat($this.attr("rating-max"));
            if ($this.attr("rating-totalvalue")) o.totalValue = parseFloat($this.attr("rating-totalvalue"));
            if ($this.attr("rating-count")) o.count = parseFloat($this.attr("rating-count"));
            if ($this.attr("rating-split")) o.split = parseFloat($this.attr("rating-split"));
            if ($this.attr("rating-value")) o.value = parseFloat($this.attr("rating-value"));
            if ($this.attr("rating-disabled")) o.disabled = $this.attr("rating-disabled") == "true";
            o.resetButton.disabled = true;
            $this = $this.wijrating(o);
        });
    }
    $(function () {
        initRatings();
        //提交评分
        $(".urating_category_my_rating .submit[submit-url]").live("click", function () {
            var $this = $(this);
            if ($this.attr("uploading") != "true") {
                $this.attr("uploading", "true");
                var $myRatingBox = $this.parents(".urating_category_my_rating");

                var values = new Array();
                var hasErr = false;
                $myRatingBox.find(".uRating-rating").each(function () {
                    var $rating = $(this);
                    var value = $rating.wijrating("option", "value");
                    if (value == 0) {
                        hasErr = true;
                        return;
                    }
                    values.push($rating.wijrating("option", "value"));
                });
                if (hasErr) {
                    alert("请为所有的评分项评分后,再尝试提交!");
                    $this.removeAttr("uploading");
                    return;
                }
                showDialog('', 'info', '<img src="' + IMGDIR + '/loading.gif"> 请稍候...');
                $.post($this.attr("submit-url"), "&ratings=" + values.join("&ratings="), function (result) {
                    $this.removeAttr("uploading");
                    if (result.success) {
                        showDialog("感谢您的评分!", "notice");
                        //刷新页面中的评分模块
                        var targetBlocks = new Array();
                        $(".urating-block[id=" + $myRatingBox.attr("id") + "][obj-id=" + $myRatingBox.attr("obj-id") + "]").each(function () {
                            var $this = $(this);
                            if ($this.parents(".urating-block").size() == 0) {
                                targetBlocks.push(this);
                            }
                        });
                        var blockContents = new Array();
                        var onFinish = initRatings;
                        $.each(targetBlocks, function (i) {
                            var $this = $(this);
                            if (blockContents[$this.attr("data-url")]) {
                                var $html = $(blockContents[$this.attr("data-url")]);
                                $this.after($html.clone()).remove();
                                if (i + 1 == targetBlocks.length) onFinish();
                            } else {
                                $.post($this.attr("data-url"), function (html) {
                                    blockContents[$this.attr("data-url")] = html;
                                    var $html = $(html);
                                    $this.after($html.clone()).remove();
                                    if (i + 1 == targetBlocks.length) onFinish();
                                });
                            }
                        });
                    } else {
                        showDialog(result.message);
                    }
                });
            }
        });
    });
})(jQuery);